using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Rensoft.CRM.Client.AccountsAPI;

namespace Rensoft.CRM.Client.Editors
{
    public partial class DomainEditor : BaseEditor
    {
        protected Account SelectedAccount
        {
            get
            {
                AccountComboItem item = (AccountComboItem)clientComboBox.SelectedItem;
                return item.Value;
            }
        }

        public DomainEditor(Program program) : base(program)
        {
            InitializeComponent();

            SetMode(EditorMode.Create);
            Text = "New Domain - " + AssemblyInfo.Title;

            expiryMonthCalendar.SelectionStart = DateTime.Now.AddYears(1);
        }

        public DomainEditor(Program program, DomainService domain) : this(program)
        {
            SetMode(EditorMode.Modify);
            Text = domain.Title + " - " + AssemblyInfo.Title;
            
            titleTextBox.Text = domain.Title;
            registerMonthCalendar.SelectionStart = domain.RegisterDate;
            expiryMonthCalendar.SelectionStart = domain.ExpiryDate;
        }

        protected override void PerformClear()
        {
            base.PerformClear();

            registerMonthCalendar.SelectionStart = DateTime.Now;
            expiryMonthCalendar.SelectionStart = DateTime.Now.AddYears(1);
            titleTextBox.ResetText();
        }

        protected override void PerformLoad(DoWorkEventArgs e)
        {
            base.PerformLoad(e);

            InitializeAccountComboBox(clientComboBox, AccountType.Client);

            switch (Mode)
            {
                case EditorMode.Modify:
                    {
                        SaveToolStripButton.Enabled = false;
                        SaveNewToolStripButton.Enabled = false;
                        SaveCloseToolStripButton.Enabled = false;
                    }
                    break;
            }
        }

        protected override void PerformSave(DoWorkEventArgs e)
        {
            AccountsService service = AccountsService.CreateInstance();

            switch (Mode)
            {
                case EditorMode.Create:
                    {
                        service.DomainCreate(
                            titleTextBox.Text,
                            registerMonthCalendar.SelectionStart,
                            expiryMonthCalendar.SelectionStart,
                            SelectedAccount.AccountId);
                    }
                    break;
            }

            base.PerformSave(e);
        }
    }
}

